<template>
  <div class="container-article">
    <el-card>
      <div slot="header" class="clearfix">
        <my-bread>内容管理</my-bread>
      </div>
      <el-form labal-width="80px" size="small">
        <el-form-item labal="状态:">
          <el-radio-group v-model="filterData.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item labal="频道:">
         <my-channel v-model="filterData.channel_id"></my-channel>
        </el-form-item>
        <el-form-item labal="日期:">
          <el-date-picker
            v-model="dateArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search()">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <div slot="header">根据筛选条件共搜索到{{total}}条数据</div>
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" alt>
              <div slot="error">
                <img src="../../assets/error.gif" alt style="width:150px;height:100px;" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status===1">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="pubdate"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button @click="editArticle(scope.row.id)" type="primary" plain icon="el-icon-edit" circle></el-button>
            <el-button @click="delArticle(scope.row.id)" type="success" plain icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="pager"
        :page-size="filterData.per_page"
        :current-page="filterData.page"
        layout="prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "app-article",
  data() {
    return {
      articles: [],
      total: 0,
      filterData: {
        status: null,
        channe_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1,
        per_page: 20
      },
      channelOptions: [],
      dateArr: []
    };
  },
  created() {
    this.getArticles();
  },
  methods: {
    delArticle(id) {
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          try{
          await this.$http.delete(`articles/${id}`)
          this.$message.success('删除成功')
          this.getArticles()
          } catch(e) {
            this.$message.error('删除失败')
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    editArticle(id) {
      this.$router.push(`/publish?id=${id}`)
    },
    async getArticles() {
      const res = await this.$http.get("articles", { params: this.filterData });
      this.articles = res.data.data.results;
      this.total = res.data.data.total_count;
    },
    pager(newPage) {
      this.filterData.page = newPage;
      this.getArticles();
    },
    changeDate(dateArr) {
      if (dateArr) {
        this.filterData.begin_pubdate = dateArr[0];
        this.filterData.end_pubdate = dateArr[1];
      } else {
        this.filterData.begin_pubdate = null;
        this.filterData.end_pubdate = null;
      }
    },
    search() {
      this.filterData.page = 1;
      this.getArticles();
    }
  }
};
</script>

<style lang="less">
</style>